$(function() {
    //获取信息 地址传到模板地址
    //储存收货地址
    let address = null;

    $.ajax({
        url: "/address/queryAddress",
        type: "get",
        success: function(res) {
            console.log(res);
            address = res;
            var html = template("addressTpl", { result: address });
            $("#address-box").html(html)
        }
    });

    //删除收货地址
    $("#address-box").on("tap", ".delete-btn", function() {

        let id = this.getAttribute("data-id");
        let li = this.parentNode.parentNode;
        console.log(id);
        mui.confirm("你确实定要删除吗？", function(message) {

            if (message.index == 1) {
                console.log(message.index);
                $.ajax({
                    url: "/address/deleteAddress",
                    type: "post",
                    data: {
                        id: id
                    },
                    success: function(res) {
                        console.log(res);
                        if (res.success) {
                            location.reload();
                            console.log("删除成功")
                        }
                    }
                });
            } else {
                console.log(message.index);
                mui.swipeoutClose(li);
                console.log("没有删除成功");
            }
        })

    });
    $("#address-box").on("tap", ".edit-btn", function() {
        // console.log(".edit-btn");
        let id = this.getAttribute("data-id");
        // console.log(id);
        // console.log(address);

        for (let i = 0; i < address.length; i++) {
            if (address[i].id == id) {
                localStorage.setItem("editAddress", JSON.stringify(address[i]));
                console.log(localStorage);
                console.log(id);
                break;
            }
        }
        //跳转页面
        location.href = "addAddress.html?isEdit=1"

    });
})